package com.dtagri.fjwlw.ui;

import com.android.nvssdklib.NvssdkAPI;
import com.android.nvssdklib.PublicDefine;
import com.dtagri.fjwlw.R;
import com.dtagri.fjwlw.bean.tdvideo.VideoInfo;
import com.dtagri.fjwlw.bean.tdvideo.cls_Channel;
import com.dtagri.fjwlw.bean.tdvideo.cls_Host;
import com.dtagri.fjwlw.ui.tdvideo.CLS_Check;
import com.dtagri.fjwlw.ui.tdvideo.CLS_VideoViewController;











import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;


/**
 * 天地伟业
 * @author Administrator 
 * 创建日期： 2015-12-29
 */
public class Show_Video_View extends Activity {

	private static final int siAddVideo = 1;
	private static final int siDownload = 2;
	private static boolean sblInitSDK = false; // 记录是否初始化SDK
	private LinearLayout m_LayoutSeekBar;
	private boolean m_bOpenPTZControl = false; // 是否打开云台控制，默认云台控制关闭
	private boolean m_bFullScreen = false; // 当前是否全屏，默认不是全屏
	private boolean m_bViewLandButton = false; // 显示横屏悬浮按钮
	private boolean m_bControlStep = false; // 云台速度控制
	private boolean m_bZoom = false; // 变倍
	private boolean m_bBright = false; // 亮度调节
	private boolean m_bFocus = false; // 聚焦
	private boolean m_b3DState = false; // 3D定位使能
	private LayoutParams m_video_Layout; // 记录窗口大小及坐标
	private LinearLayout m_LayoutBody;
	private boolean m_bFirstCreate = true; // 标示当前第一次打开
	// 滑动条变量声明
	private SeekBar m_seekbarControlStep;
	private TextView m_textControlValue, m_textVideoDeviceName;
	private LayoutInflater inflater;
	/** 放大缩小按钮控件 */
	private ImageButton m_btnLargen, m_btnShorten;
	/** 聚焦近，聚焦远按钮控件 */
	private ImageButton m_btnFocusNear, m_btnFocusFar;
	// 预置位名称控件
	private EditText m_EditPresetName;

	// 悬浮按钮
	private ImageButton m_ibtnCaptureLand, m_ibtnPTZLand;// ,m_ibtnFullScreen;//m_ibtnVideoVolumeland
	private LinearLayout m_layoutLand, m_layoutBottom, m_layoutOver;
	// 最底部一层按钮
	private ImageButton ibtnVideo_Ptz;
	// 第二层按钮
	private ImageButton ibtnChild_Multiple, ibtnChild_Preset, ibtnChild_Bright,
			ibtmChild_focalize, ibtnChild_PTZSpeed;
	private Button ibtn_video_Logoff;
	// 云台控制：上、下、左、右
	private ImageButton btnPtz_up, btnPtz_down, btnPtz_left, btnPtz_right;

	// 当前系统分辨率
	private int m_iWindowWidth, m_iWindowHeight, m_BodyHeight;

	// 记录视频窗口普通和全屏是的宽和高
	private int m_iNormalVideoWidth, m_iNormalVideoHeight, m_iFullVideoWidth,
			m_iFullVideoHeight;
	// 四个视频窗口
	private VView Real_Time_video1;
	private ImageView Real_Time_video_Frame;

	// 弹出对话框
	private AlertDialog.Builder m_AlertAddPreset;

	private ProgressBar m_ViewProgress;

	/** 视频控制 */
	private CLS_VideoViewController m_cls_VideoViewController;
	/** 记录按下屏幕时的坐标 */
	private int m_iPreX, m_iPreY, m_iAfterX, m_iAfterY;
	public Handler messageHandler;
	public MsgHandler msgHandler;
	private cls_Host cls_SelectHost = null;
	private cls_Channel cls_SelectChannel = null;

	int iface = 1; // 返回页面标记
	String areaId, areaName;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.video_view_show);
//		 得到当前线程的Looper实例，由于当前线程是UI线程也可以通过Looper.getMainLooper()得到
		Looper looper = Looper.myLooper();

		// 此处甚至可以不需要设置Looper，因为 Handler默认就使用当前线程的Looper
		msgHandler = new MsgHandler(looper);

		InitVaraible();

		InitSDK();

		btnEvent();

		SuitScreen();

		// 获取视频参数
		bindingVideoParam();
	}

	// 获取视频参数
	void bindingVideoParam() {
		Intent gi = getIntent();
		VideoInfo entity = (VideoInfo) gi
				.getSerializableExtra("video_param_infos");
		iface = gi.getIntExtra("video_interface", 1);
		areaId = gi.getStringExtra("cur_area_id");
		areaName = gi.getStringExtra("cur_area_name");
		// entity.setVideoIp("192.168.16.224");
		cls_SelectHost = new cls_Host();
		cls_SelectHost.setHostID(entity.getHostID()); // 11
		cls_SelectHost.setHostName(entity.getVideoIp());
		cls_SelectHost.setConnectTypeID(0);
		cls_SelectHost.setChannelNumber(1);
		cls_SelectHost.setConnectIP(entity.getVideoIp());
		cls_SelectHost.setUserName(entity.getVideoUsername());
		cls_SelectHost.setPassword(entity.getVideoPwd());
		cls_SelectHost.setConnectPort(entity.getVideoPort());
		cls_SelectHost.setDeviceID("");

		cls_SelectChannel = new cls_Channel();
		cls_SelectChannel.setChannelName("channel" + entity.getVideoChannel());
		cls_SelectChannel.setProtocol(String.valueOf(entity.getVideoBaud()));
		cls_SelectChannel.setChannelID(1);
		cls_SelectChannel.setChannelNo(entity.getVideoChannel());
		cls_SelectChannel.setComNumber(entity.getVideoComNum());
		cls_SelectChannel.setControlAddress(entity.getVideoAddress());
		cls_SelectChannel.setControlStep(entity.getVideoStep());
		cls_SelectChannel.setHostID(entity.getHostID());
		cls_SelectChannel.setRecommendID(111);
		cls_SelectChannel.setRecTime(111);
		cls_SelectChannel.setPTZType(entity.getVideoProtocol());
		cls_SelectChannel.SetStreamNo(entity.getVideoStream());
		// 由于现在球机无法控制，故将控制这块屏蔽掉
		// if(entity.getType()==0)
		// ibtnVideo_Ptz.setVisibility(View.VISIBLE);
		// else
		ibtnVideo_Ptz.setVisibility(View.GONE);
		// m_textVideoDeviceName.setText("设备载入成功!");
		Toast.makeText(getApplicationContext(), "设备加载成功!", 500);

		// 登录
		if (cls_SelectHost == null || cls_SelectChannel == null) {

			Toast.makeText(Show_Video_View.this, R.string.Noate_Add_Device,
					Toast.LENGTH_SHORT).show();
			return;
		}
		m_cls_VideoViewController.LogonSelctedChannel(cls_SelectHost,
				cls_SelectChannel, Real_Time_video1);
		// 视频播放见1281行
	}

	// 停止视频播放函数
	private void StopView() {
		if (m_cls_VideoViewController.StopVideo()) {
			m_textVideoDeviceName.setText(getString(R.string.Notes_No_Video));
			if (m_bOpenPTZControl) {
				PTZControlChildButtonView();
			}
			m_ViewProgress.setVisibility(View.INVISIBLE);
		}
	}

	// 视频显示适应窗口
	private void SuitScreen() {
		WindowManager manager = getWindowManager();
		m_iWindowWidth = manager.getDefaultDisplay().getWidth();
		m_iWindowHeight = manager.getDefaultDisplay().getHeight();
		m_iFullVideoWidth = m_iWindowWidth;

		// -------------------------------------------
		m_bFullScreen = true;
		// -------------------------------------------

		if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			m_BodyHeight = m_iWindowHeight;
			m_LayoutBody.setLayoutParams(new LinearLayout.LayoutParams(
					m_iFullVideoWidth, m_iWindowHeight));

			SwitchToLandScape();
		} else {
			m_BodyHeight = m_iWindowHeight - 340;
			m_LayoutBody.setLayoutParams(new LinearLayout.LayoutParams(
					m_iFullVideoWidth, m_BodyHeight));
			BackToHorrent();
		}
		// 实际中间列表的高度

		m_iFullVideoHeight = m_BodyHeight;
		m_iNormalVideoWidth = (int) m_iWindowWidth / 2 - 1;
		m_iNormalVideoHeight = (int) m_BodyHeight / 2;

		m_cls_VideoViewController.SetViewSize(m_iWindowWidth, m_iWindowHeight,
				m_BodyHeight, m_iNormalVideoWidth, m_iNormalVideoHeight);

		// alter by cpx 110305
		if (m_cls_VideoViewController.HasPlayed() || m_bFirstCreate) {
			m_bFirstCreate = false;
		}
		Real_Time_video1.SetCurrentScreenSize(m_iFullVideoWidth,
				m_iFullVideoHeight);
		// end alter

		LayoutParams Progress_Layout = new AbsoluteLayout.LayoutParams(40, 40,
				(int) (m_iFullVideoWidth / 2) - 20, (int) (m_BodyHeight / 2));
		m_ViewProgress.setLayoutParams(Progress_Layout);
		if (m_bFullScreen) {
			OneVideoFullScreen();
		}

		if (m_bOpenPTZControl) {
			PTZChildButtonView();
			VisibleOfDirection(true);
		} else {
			// 缩小至普通大小
			NormalChildButtonView();
			NormalChildButtonView();
			VisibleOfDirection(false);
		}

		return;

	}

	// 初始化变量和控件
	@SuppressWarnings("deprecation")
	private void InitVaraible() {
		// 关联按钮变量
		m_LayoutBody = (LinearLayout) this
				.findViewById(R.id.Video_View_Body_Layout);

		// ibtn_video_Play = (Button)this.findViewById(R.id.btn_video_Play);
		// ibtnVideo_Stop = (Button)this.findViewById(R.id.btn_video_stop);
		// ibtnVideo_Download =
		// (Button)this.findViewById(R.id.btn_video_download);
		// ibtnVideo_Capture =
		// (ImageButton)this.findViewById(R.id.btn_video_capture);
		ibtnVideo_Ptz = (ImageButton) this.findViewById(R.id.btn_video_ptz);

		// ibtnChild_Switch = (Button)this.findViewById(R.id.btn_video_switch);
		// ibtn_video_Logon = (Button)this.findViewById(R.id.btn_video_Logon);
		ibtn_video_Logoff = (Button) this.findViewById(R.id.btn_video_Logoff);

		ibtnChild_Multiple = (ImageButton) this
				.findViewById(R.id.btn_video_multiple);
		ibtnChild_Preset = (ImageButton) this
				.findViewById(R.id.btn_video_preset);
		ibtnChild_Bright = (ImageButton) this
				.findViewById(R.id.btn_video_bright);
		ibtmChild_focalize = (ImageButton) this
				.findViewById(R.id.btn_video_focalize);
		ibtnChild_PTZSpeed = (ImageButton) this
				.findViewById(R.id.btn_video_PTZ_Speed);

		m_btnLargen = (ImageButton) this.findViewById(R.id.Video_View_Largen);
		m_btnShorten = (ImageButton) this.findViewById(R.id.Video_View_Shorten);
		m_btnFocusNear = (ImageButton) this
				.findViewById(R.id.Video_View_Focus_Near);
		m_btnFocusFar = (ImageButton) this
				.findViewById(R.id.Video_View_Focus_Far);

		// 滑动条控件注册
		m_seekbarControlStep = (SeekBar) this
				.findViewById(R.id.progress_Control_Step);
		m_textControlValue = (TextView) this
				.findViewById(R.id.Video_View_Control_Step_Text);
		m_textControlValue.setTextColor(android.graphics.Color.WHITE);
		m_LayoutSeekBar = (LinearLayout) this
				.findViewById(R.id.Video_View_SeekBar_Layout);

		m_ibtnCaptureLand = (ImageButton) this
				.findViewById(R.id.btn_video_capture_landscape);
		m_ibtnPTZLand = (ImageButton) this
				.findViewById(R.id.btn_video_ptz_landscape);
		m_layoutLand = (LinearLayout) this
				.findViewById(R.id.video_bottom_button_landscape_layout);
		m_layoutBottom = (LinearLayout) this
				.findViewById(R.id.video_bottom_button_layout);
		m_layoutOver = (LinearLayout) this
				.findViewById(R.id.video_over_button_layout);

		// 视频播放控件关联
		Real_Time_video1 = (VView) this
				.findViewById(R.id.Real_Time_view_video1);
		Real_Time_video_Frame = (ImageView) this
				.findViewById(R.id.Real_Time_View_Frame);
		m_ViewProgress = (ProgressBar) this.findViewById(R.id.View_Progress);
		m_textVideoDeviceName = (TextView) this
				.findViewById(R.id.Text_Device_Name);

		// 云台方向控制关联
		btnPtz_up = (ImageButton) this.findViewById(R.id.Ptz_up);
		btnPtz_down = (ImageButton) this.findViewById(R.id.Ptz_down);
		btnPtz_left = (ImageButton) this.findViewById(R.id.Ptz_left);
		btnPtz_right = (ImageButton) this.findViewById(R.id.Ptz_right);

		m_video_Layout = Real_Time_video1.getLayoutParams();

		m_seekbarControlStep.setVisibility(View.INVISIBLE);
		m_textControlValue.setVisibility(View.INVISIBLE);
		m_LayoutSeekBar.setVisibility(View.INVISIBLE);

		// 添加收藏提示框
		m_AlertAddPreset = new AlertDialog.Builder(this);

		m_cls_VideoViewController = CLS_VideoViewController.GetInstance();// new
																			// CLS_VideoViewController();
//		m_cls_VideoViewController.Registe(this);

		// 预置位
		inflater = (LayoutInflater) this
				.getSystemService(LAYOUT_INFLATER_SERVICE);

		VisibleOfDirection(false); // 隐藏四个方向按钮
		// 默认横屏悬浮按钮不显示
		m_ibtnCaptureLand.setVisibility(View.INVISIBLE);
		m_ibtnPTZLand.setVisibility(View.INVISIBLE);
		m_layoutLand.setVisibility(View.INVISIBLE);
		// 隐藏放大缩小按钮
		ViewCameraButton(0);

		m_ViewProgress.setVisibility(View.INVISIBLE);

		// 3D定位坐标初始化
		m_iPreX = -1;
		m_iPreY = -1;
		m_iAfterX = -1;
		m_iAfterY = -1;

	}

	private void InitSDK() {
		if (!sblInitSDK) // 初始化SDK的函数
		{
			NvssdkAPI.GetInstance().NetClient_Startup(3000, 3000);
			sblInitSDK = true;
		}

		NvssdkAPI.GetInstance().NetClient_SetNotifyFunction(
				m_cls_VideoViewController);
	}

	// 单击事件注册函数
	private void btnEvent() {
		// 登录设备
		/*
		 * ibtn_video_Logon.setOnClickListener(new OnClickListener() {
		 * 
		 * public void onClick(View v) { if(cls_SelectHost == null ||
		 * cls_SelectChannel == null){ Toast.makeText(Show_Video_View.this,
		 * R.string.Noate_Add_Device, Toast.LENGTH_SHORT).show(); return; }
		 * m_cls_VideoViewController
		 * .LogonSelctedChannel(cls_SelectHost,cls_SelectChannel
		 * ,Real_Time_video1); } });
		 */
		// 登出设备
		ibtn_video_Logoff.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				m_cls_VideoViewController.Logoff();
				m_textVideoDeviceName
						.setText(getString(R.string.Notes_Logoff_Success));
				m_ViewProgress.setVisibility(View.INVISIBLE);
			}
		});
		// 开始播放
		/*
		 * ibtn_video_Play.setOnClickListener(new OnClickListener() {
		 * 
		 * public void onClick(View v) { if(cls_SelectHost == null ||
		 * cls_SelectChannel == null){ Toast.makeText(Show_Video_View.this,
		 * R.string.Noate_Add_Device, Toast.LENGTH_SHORT).show(); return; }
		 * if(!m_cls_VideoViewController.HasRev()){
		 * Toast.makeText(Show_Video_View.this, R.string.Noate_Logon_Device,
		 * Toast.LENGTH_SHORT).show(); return; }
		 * m_cls_VideoViewController.PlaySelctedChannel(); } });
		 * 
		 * //停止视频播放 ibtnVideo_Stop.setOnClickListener(new OnClickListener() {
		 * 
		 * public void onClick(View v) { StopView(); } }); //下载
		 * ibtnVideo_Download.setOnClickListener(new OnClickListener() {
		 * 
		 * public void onClick(View v) {
		 * 
		 * Intent intent = new Intent(); intent.setClass(Show_Video_View.this,
		 * Show_Download_View.class); startActivity(intent); } }); //抓拍
		 * ibtnVideo_Capture.setOnClickListener(new OnClickListener() {
		 * 
		 * public void onClick(View v) {
		 * //m_cls_VideoViewController.Set3GDialogStart();
		 * if(!m_cls_VideoViewController.HasPlayed()) return;
		 * if(m_cls_VideoViewController.GetSnapshotCapture()) {
		 * Toast.makeText(Show_Video_View.this,
		 * R.string.Capture_Button_Suc,Toast.LENGTH_SHORT).show(); } else {
		 * Toast.makeText(Show_Video_View.this,
		 * R.string.Capture_Button_Fail,Toast.LENGTH_SHORT).show(); }
		 * 
		 * } }); //横屏图像抓拍 m_ibtnCaptureLand.setOnClickListener(new
		 * OnClickListener() {
		 * 
		 * public void onClick(View v) {
		 * if(!m_cls_VideoViewController.HasPlayed()) return;
		 * NormalChildButtonView();
		 * if(m_cls_VideoViewController.GetSnapshotCapture()) {
		 * Toast.makeText(Show_Video_View.this,
		 * R.string.Capture_Button_Suc,Toast.LENGTH_SHORT).show(); } else {
		 * Toast.makeText(Show_Video_View.this,
		 * R.string.Capture_Button_Fail,Toast.LENGTH_SHORT).show(); } } });
		 */
		// 云台控制
		ibtnVideo_Ptz.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// m_cls_VideoViewController.Set3GDialogStop();
				if (!m_cls_VideoViewController.HasPlayed())
					return;
				PTZControlChildButtonView();

			}
		});
		// 云台控制
		m_ibtnPTZLand.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (!m_cls_VideoViewController.HasPlayed())
					return;
				if (!m_bFullScreen) {
					OneVideoFullScreen();
				}
				if (m_bOpenPTZControl) {
					VisibleOfDirection(false);
					m_bOpenPTZControl = false;
					m_b3DState = false;
					ibtnVideo_Ptz.setImageResource(R.drawable.video_kongzhi);
					m_ibtnPTZLand.setImageResource(R.drawable.video_kongzhi);
				} else {
					VisibleOfDirection(true);
					m_bOpenPTZControl = true;
					m_b3DState = true;
					ibtnVideo_Ptz.setImageResource(R.drawable.video_kongzhi_s);
					m_ibtnPTZLand.setImageResource(R.drawable.video_kongzhi_s);
				}
			}
		});

		// 光圈调节 --alter by cpx 110306 光圈使用放大缩小按钮
		ibtnChild_Bright.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (m_bBright) {
					// 按钮变灰
					ViewCameraButton(0);
				} else {
					ViewCameraButton(3);
				}
			}
		});

		// 视频切换
		/*
		 * ibtnChild_Switch.setOnClickListener(new OnClickListener() {
		 * 
		 * public void onClick(View v) {
		 * if(m_cls_VideoViewController.HasPlayed()) return; Intent intent = new
		 * Intent(); intent.setClass(Show_Video_View.this,
		 * Show_Device_Info.class); startActivityForResult(intent, siAddVideo);
		 * //m_iSelectView } });
		 */

		// 变倍操作
		ibtnChild_Multiple.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (m_bZoom) {
					ViewCameraButton(0);
				} else {
					ViewCameraButton(1);
				}
			}
		});
		// 变倍大
		m_btnLargen.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (m_bZoom) // 当为変倍时
				{
					if (event.getAction() == MotionEvent.ACTION_DOWN) {
						m_cls_VideoViewController
								.ControlPTZ(PublicDefine.ZOOM_BIG);
					}
					if (event.getAction() == MotionEvent.ACTION_UP) {
						m_cls_VideoViewController
								.ControlPTZ(PublicDefine.ZOOM_BIG_STOP);
					}
				}

				if (m_bBright) // 当为光圈时
				{
					if (event.getAction() == MotionEvent.ACTION_DOWN) {
						m_cls_VideoViewController
								.ControlPTZ(PublicDefine.IRIS_OPEN);
					}
					if (event.getAction() == MotionEvent.ACTION_UP) {
						m_cls_VideoViewController
								.ControlPTZ(PublicDefine.IRIS_OPEN_STOP);
					}
				}

				return false;
			}
		});
		// 变倍小
		m_btnShorten.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (m_bZoom) // 当为変倍时
				{
					if (event.getAction() == MotionEvent.ACTION_DOWN) {
						m_cls_VideoViewController
								.ControlPTZ(PublicDefine.ZOOM_SMALL);
					}
					if (event.getAction() == MotionEvent.ACTION_UP) {
						m_cls_VideoViewController
								.ControlPTZ(PublicDefine.ZOOM_SMALL_STOP);
					}
				}

				if (m_bBright) // 当为光圈时
				{
					if (event.getAction() == MotionEvent.ACTION_DOWN) {
						m_cls_VideoViewController
								.ControlPTZ(PublicDefine.IRIS_CLOSE);
					}
					if (event.getAction() == MotionEvent.ACTION_UP) {
						m_cls_VideoViewController
								.ControlPTZ(PublicDefine.IRIS_CLOSE_STOP);
					}
				}

				return false;
			}
		});
		// 预置位操作
		ibtnChild_Preset.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				View textEntryView = inflater.inflate(
						R.layout.video_username_modify_item, null);

				m_EditPresetName = (EditText) textEntryView
						.findViewById(R.id.UserName_Modify);
				// 文本框输入合法性判断
				m_EditPresetName.addTextChangedListener(new TextWatcher() {
					@Override
					public void onTextChanged(CharSequence s, int start,
							int before, int count) {
					}

					@Override
					public void beforeTextChanged(CharSequence s, int start,
							int count, int after) {

					}

					@Override
					public void afterTextChanged(Editable s) {
						if (s.length() > 0) {
							if (!CLS_Check.CheckNum(s.toString())) {
								s.delete(s.length() - 1, s.length());
							} else {
								int iPreset = Integer.parseInt(s.toString());
								if (iPreset < 0)
									return;
								if (iPreset > 255 || iPreset < 1)
									s.delete(s.length() - 1, s.length());
							}
						}
					}
				});
				m_AlertAddPreset.setView(textEntryView);
				m_AlertAddPreset.setPositiveButton(
						getString(R.string.Preset_Button_Set),
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								String strTemp = m_EditPresetName.getText()
										.toString().trim();
								if (strTemp.length() == 0)
									return;
								m_cls_VideoViewController.SetPresetPara(Integer
										.parseInt(strTemp));
								m_cls_VideoViewController
										.ControlPTZ(PublicDefine.SET_VIEW);
							}
						}).setNegativeButton(
						getString(R.string.Preset_Button_Call),
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								String strTemp = m_EditPresetName.getText()
										.toString().trim();
								if (strTemp.length() == 0)
									return;
								m_cls_VideoViewController.SetPresetPara(Integer
										.parseInt(strTemp));
								m_cls_VideoViewController
										.ControlPTZ(PublicDefine.CALL_VIEW);
							}
						});
				ViewCameraButton(0);
				m_AlertAddPreset.show();

			}

		});
		// 云台速度控制
		ibtnChild_PTZSpeed.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (m_bControlStep) {
					ViewCameraButton(0);
					m_b3DState = true;

				} else {
					ViewCameraButton(5);
					m_b3DState = false;
				}
			}
		});
		// 控制步长滑动条操作
		m_seekbarControlStep
				.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

					@Override
					public void onStopTrackingTouch(SeekBar seekBar) {
						m_cls_VideoViewController.SetPTZSpeed(seekBar
								.getProgress());

					}

					@Override
					public void onStartTrackingTouch(SeekBar seekBar) {
					}

					@Override
					public void onProgressChanged(SeekBar seekBar,
							int progress, boolean fromUser) {
						m_textControlValue.setText(progress + "");
						m_cls_VideoViewController.SetPTZSpeed(progress);
					}
				});
		// 聚焦控制
		ibtmChild_focalize.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (m_bFocus) {
					ViewCameraButton(0);
					// m_cls_VideoViewController.CameraControl(PublicDefine)
				} else {
					ViewCameraButton(4);
				}
			}
		});
		// 聚焦近
		m_btnFocusNear.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					m_cls_VideoViewController
							.ControlPTZ(PublicDefine.FOCUS_NEAR);
				}
				if (event.getAction() == MotionEvent.ACTION_UP) {
					m_cls_VideoViewController
							.ControlPTZ(PublicDefine.FOCUS_NEAR_STOP);
				}
				return false;
			}
		});
		// 聚焦远
		m_btnFocusFar.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					m_cls_VideoViewController
							.ControlPTZ(PublicDefine.FOCUS_FAR);
				}
				if (event.getAction() == MotionEvent.ACTION_UP) {
					m_cls_VideoViewController
							.ControlPTZ(PublicDefine.FOCUS_FAR_STOP);
				}
				return false;
			}
		});
		// 云台控制方向按钮单击事件
		btnPtz_up.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					if (!m_cls_VideoViewController
							.ControlPTZ(PublicDefine.MOVE_UP))
						;
				}
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (m_cls_VideoViewController
							.ControlPTZ(PublicDefine.MOVE_UP_STOP))
						;
				}
				return false;
			}
		});

		btnPtz_down.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					if (m_cls_VideoViewController
							.ControlPTZ(PublicDefine.MOVE_DOWN))
						;
				}
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (m_cls_VideoViewController
							.ControlPTZ(PublicDefine.MOVE_DOWN_STOP))
						;
				}
				return false;
			}
		});

		btnPtz_left.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					if (m_cls_VideoViewController
							.ControlPTZ(PublicDefine.MOVE_LEFT))
						;

				}
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (m_cls_VideoViewController
							.ControlPTZ(PublicDefine.MOVE_LEFT_STOP))
						;

				}
				return false;
			}
		});

		btnPtz_right.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					if (m_cls_VideoViewController
							.ControlPTZ(PublicDefine.MOVE_RIGHT))
						;

				}
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (m_cls_VideoViewController
							.ControlPTZ(PublicDefine.MOVE_RIGHT_STOP))
						;
				}
				return false;
			}
		});

		Real_Time_video1.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int iAction = event.getAction();

				if (iAction == MotionEvent.ACTION_DOWN) {
					m_iPreX = (int) event.getX();
					m_iPreY = (int) event.getY();
				}
				if (iAction == MotionEvent.ACTION_MOVE) {
					m_iAfterX = (int) event.getX();
					m_iAfterY = (int) event.getY();
				}

				if (iAction == MotionEvent.ACTION_UP) {
					if (m_iPreX < 0 || m_iPreY < 0) {
						return false;
					}
					if (m_iAfterX < 0 || m_iAfterY < 0) {
						return false;
					}
					if ((Math.abs(m_iAfterX - m_iPreX) < 20)
							&& (Math.abs(m_iAfterY - m_iPreY) < 20)) {
						ViewLandScapeButton();
						return false;
					}
					if (m_bOpenPTZControl && m_b3DState) {
						Orientation();
					}
					return true;
				}

				// 点击事件，显示横屏工具栏
				// if(m_iAfterX != m_iPreX && m_iAfterY != m_iPreY)

				if (m_cls_VideoViewController.SetSelectedView(0)) {
					LayoutParams video_Frame_Layout = Real_Time_video1
							.getLayoutParams();
					Real_Time_video_Frame.setLayoutParams(video_Frame_Layout);
				}

				return false;
			}
		});

	}

	// 全屏事件处理
	private void FullScreenActive() {
		OneVideoFullScreen();
		// this.getResources().getConfiguration().orientation =
		// Configuration.ORIENTATION_LANDSCAPE;
		// this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	}

	// 显示滑动条SeekBar
	private void ViewSeekBar(boolean _bVisible) {
		if (_bVisible) {
			m_seekbarControlStep.setVisibility(View.VISIBLE);
			m_textControlValue.setVisibility(View.VISIBLE);
			m_LayoutSeekBar.setVisibility(View.VISIBLE);
		} else {
			m_seekbarControlStep.setVisibility(View.INVISIBLE);
			m_textControlValue.setVisibility(View.INVISIBLE);
			int iProgress = m_cls_VideoViewController.GetPtzSpeed();
			m_textControlValue.setText(iProgress + "");
			m_seekbarControlStep.setProgress(iProgress);
			m_LayoutSeekBar.setVisibility(View.INVISIBLE);
		}
	}

	// 竖屏显示默认按钮
	private void NormalChildButtonView() {
		// ibtnChild_Switch.setVisibility(View.VISIBLE);
		// ibtn_video_Logon.setVisibility(View.VISIBLE);
		ibtn_video_Logoff.setVisibility(View.VISIBLE);
		ibtnChild_Multiple.setVisibility(View.INVISIBLE);
		ibtnChild_Preset.setVisibility(View.INVISIBLE);
		ibtnChild_Bright.setVisibility(View.INVISIBLE);
		ibtmChild_focalize.setVisibility(View.INVISIBLE);
		ibtnChild_PTZSpeed.setVisibility(View.INVISIBLE);
		// 关闭缩放按钮

		ViewCameraButton(0);
	}

	// 显示竖屏状态下云台控制子按钮
	private void PTZChildButtonView() {
		// ibtnVideo_Volume.setVisibility(View.INVISIBLE);
		// ibtnChild_Switch.setVisibility(View.INVISIBLE);
		// ibtn_video_Logon.setVisibility(View.INVISIBLE);
		ibtn_video_Logoff.setVisibility(View.INVISIBLE);
		// ibtnChild_Multiple.setVisibility(View.VISIBLE);
		ibtnChild_Preset.setVisibility(View.VISIBLE);
		ibtnChild_Bright.setVisibility(View.VISIBLE);
		ibtmChild_focalize.setVisibility(View.VISIBLE);
		ibtnChild_PTZSpeed.setVisibility(View.VISIBLE);

		VisibleOfDirection(false);
	}

	private void OneVideoFullScreen() {
		LayoutParams video_Layout = new AbsoluteLayout.LayoutParams(
				m_iFullVideoWidth, m_BodyHeight, 0, 0);
		m_video_Layout = Real_Time_video1.getLayoutParams();
		Real_Time_video1.setVisibility(View.VISIBLE);
		Real_Time_video1.setLayoutParams(video_Layout);

		Real_Time_video_Frame.setVisibility(View.INVISIBLE);
		m_bFullScreen = true;
	}

	// 云台控制
	private void PTZControlChildButtonView() {
		if (!m_bFullScreen) {
			OneVideoFullScreen();
		}
		if (m_bOpenPTZControl) {
			// 缩小至普通大小
			NormalChildButtonView();
			VisibleOfDirection(false);
			m_bOpenPTZControl = false;
			m_b3DState = false;
			ibtnVideo_Ptz.setImageResource(R.drawable.video_kongzhi);
			m_ibtnPTZLand.setImageResource(R.drawable.video_kongzhi);
		} else {
			PTZChildButtonView();
			VisibleOfDirection(true);
			m_bOpenPTZControl = true;
			m_b3DState = true;
			ibtnVideo_Ptz.setImageResource(R.drawable.video_kongzhi_s);
			m_ibtnPTZLand.setImageResource(R.drawable.video_kongzhi_s);
		}

	}

	// 云台方向按钮显示控制
	private void VisibleOfDirection(boolean _bVisible) {
		if (_bVisible) {
			btnPtz_up.setVisibility(View.VISIBLE);
			btnPtz_down.setVisibility(View.VISIBLE);
			btnPtz_left.setVisibility(View.VISIBLE);
			btnPtz_right.setVisibility(View.VISIBLE);
			LayoutParams btnPtz_up_layout = new AbsoluteLayout.LayoutParams(36,
					36, m_iNormalVideoWidth - 18, 0);
			btnPtz_up.setLayoutParams(btnPtz_up_layout);
			LayoutParams btnPtz_down_layout = new AbsoluteLayout.LayoutParams(
					36, 36, m_iNormalVideoWidth - 18, m_BodyHeight - 35);
			btnPtz_down.setLayoutParams(btnPtz_down_layout);
			LayoutParams btnPtz_left_layout = new AbsoluteLayout.LayoutParams(
					36, 36, 0, m_iNormalVideoHeight - 12);
			btnPtz_left.setLayoutParams(btnPtz_left_layout);
			LayoutParams btnPtz_right_layout = new AbsoluteLayout.LayoutParams(
					36, 36, m_iWindowWidth - 36, m_iNormalVideoHeight - 12);
			btnPtz_right.setLayoutParams(btnPtz_right_layout);
		} else {
			btnPtz_up.setVisibility(View.INVISIBLE);
			btnPtz_down.setVisibility(View.INVISIBLE);
			btnPtz_left.setVisibility(View.INVISIBLE);
			btnPtz_right.setVisibility(View.INVISIBLE);
		}

	}

	// 切换横屏
	private void SwitchToLandScape() {
		m_layoutBottom.setVisibility(View.INVISIBLE);
		m_layoutOver.setVisibility(View.INVISIBLE);
		// ibtnVideo_Stop.setVisibility(View.INVISIBLE);
		// ibtnVideo_Group.setVisibility(View.INVISIBLE);
		// ibtnVideo_Color.setVisibility(View.INVISIBLE);
		// ibtnVideo_Capture.setVisibility(View.INVISIBLE);
		ibtnVideo_Ptz.setVisibility(View.INVISIBLE);

		// ibtnChild_Switch.setVisibility(View.INVISIBLE);
		// ibtnVideo_Download.setVisibility(View.VISIBLE);
		ibtnChild_Multiple.setVisibility(View.INVISIBLE);
		ibtnChild_Preset.setVisibility(View.INVISIBLE);
		ibtnChild_Bright.setVisibility(View.INVISIBLE);
		ibtmChild_focalize.setVisibility(View.INVISIBLE);
		ibtnChild_PTZSpeed.setVisibility(View.INVISIBLE);
		// 关闭缩放按钮
		ViewCameraButton(0);
	}

	// 切换竖屏
	private void BackToHorrent() {
		m_layoutBottom.setVisibility(View.VISIBLE);
		m_layoutOver.setVisibility(View.VISIBLE);
		// ibtnVideo_Stop.setVisibility(View.VISIBLE);
		// ibtnVideo_Group.setVisibility(View.VISIBLE);
		// ibtnVideo_Color.setVisibility(View.VISIBLE);
		// ibtnVideo_Capture.setVisibility(View.VISIBLE);
		ibtnVideo_Ptz.setVisibility(View.VISIBLE);

		// ibtnVideo_FullScreen.setVisibility(View.VISIBLE);

		m_bViewLandButton = false;
		m_ibtnCaptureLand.setVisibility(View.INVISIBLE);
		m_ibtnPTZLand.setVisibility(View.INVISIBLE);
		// m_ibtnFullScreen.setVisibility(View.INVISIBLE);
		m_layoutLand.setVisibility(View.INVISIBLE);

		// 显示竖屏正常子按钮
		NormalChildButtonView();
	}

	// 显示横屏悬浮按钮
	private void ViewLandScapeButton() {
		if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			if (m_bViewLandButton) {
				m_ibtnCaptureLand.setVisibility(View.INVISIBLE);
				m_ibtnPTZLand.setVisibility(View.INVISIBLE);
				// m_ibtnFullScreen.setVisibility(View.INVISIBLE);
				m_layoutLand.setVisibility(View.INVISIBLE);
				m_bViewLandButton = false;
			} else {
				m_ibtnCaptureLand.setVisibility(View.VISIBLE);
				m_ibtnPTZLand.setVisibility(View.VISIBLE);
				// m_ibtnPresetLand.setVisibility(View.VISIBLE);

				// m_ibtnFullScreen.setVisibility(View.VISIBLE);

				// m_layoutLand.setVisibility(View.VISIBLE);
				m_bViewLandButton = true;
			}

		}
	}

	// 显示云镜控制按钮显示状态
	private void ViewCameraButton(int _iActivity) {
		m_bZoom = false;
		m_btnLargen.setVisibility(View.INVISIBLE);
		m_btnShorten.setVisibility(View.INVISIBLE);
		ibtnChild_Multiple.setImageResource(R.drawable.video_fangda);
		// ibtnChild_Preset.setImageResource(R.drawable.video_yuzhiwei);
		m_bBright = false;
		ibtnChild_Bright.setImageResource(R.drawable.video_guangquan);
		m_bFocus = false;
		m_btnFocusNear.setVisibility(View.INVISIBLE);
		m_btnFocusFar.setVisibility(View.INVISIBLE);
		ibtmChild_focalize.setImageResource(R.drawable.video_jujiao);
		m_bControlStep = false;
		ibtnChild_PTZSpeed.setImageResource(R.drawable.ptzspeed);
		ViewSeekBar(false);

		switch (_iActivity) {
		case 1: // 变倍控制
			m_bZoom = true;
			m_btnLargen.setVisibility(View.VISIBLE);
			m_btnShorten.setVisibility(View.VISIBLE);
			ibtnChild_Multiple.setImageResource(R.drawable.video_fangda_s);
			break;
		case 2: // 预置位调用
			// ibtnChild_Preset.setImageResource(R.drawable.video_yuzhiwei_s);
			break;
		case 3: // 光圈控制
			m_bBright = true;
			m_btnLargen.setVisibility(View.VISIBLE);
			m_btnShorten.setVisibility(View.VISIBLE);
			ibtnChild_Bright.setImageResource(R.drawable.video_guangquan_s);
			break;
		case 4: // 聚焦控制
			m_bFocus = true;
			m_btnFocusNear.setVisibility(View.VISIBLE);
			m_btnFocusFar.setVisibility(View.VISIBLE);
			ibtmChild_focalize.setImageResource(R.drawable.video_jujiao_s);
			break;
		case 5: // 控制步长
			ViewSeekBar(true);
			m_bControlStep = true;
			ibtnChild_PTZSpeed.setImageResource(R.drawable.ptzspeed_s);
			break;
		default:
			break;
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Bundle SelectHostChannel = data.getExtras();
		if (SelectHostChannel == null)
			return;
		switch (requestCode) {
		case siAddVideo: // 添加视频

			cls_SelectHost = new cls_Host();
			cls_SelectHost.setHostID(SelectHostChannel.getInt("HostID"));
			cls_SelectHost.setHostName(SelectHostChannel.getString("HostName"));
			cls_SelectHost.setConnectTypeID(SelectHostChannel
					.getInt("ConnectTypeID"));
			cls_SelectHost.setChannelNumber(SelectHostChannel
					.getInt("ChannelNumber"));
			cls_SelectHost
					.setConnectIP(SelectHostChannel.getString("DeviceIP"));
			cls_SelectHost.setUserName(SelectHostChannel.getString("UserName"));
			cls_SelectHost.setPassword(SelectHostChannel.getString("PassWord"));
			cls_SelectHost.setConnectPort(SelectHostChannel
					.getInt("DevicePort"));
			cls_SelectHost.setDeviceID(SelectHostChannel.getString("DeviceID"));

			cls_SelectChannel = new cls_Channel();

			cls_SelectChannel.setChannelName(SelectHostChannel
					.getString("ChannelName"));
			cls_SelectChannel.setProtocol(SelectHostChannel
					.getString("Protocol"));
			cls_SelectChannel.setChannelID(SelectHostChannel
					.getInt("ChannelID"));
			cls_SelectChannel.setChannelNo(SelectHostChannel
					.getInt("ChannelNo"));
			cls_SelectChannel.setComNumber(SelectHostChannel
					.getInt("ComNumber"));
			cls_SelectChannel.setControlAddress(SelectHostChannel
					.getInt("ControlAddress"));
			cls_SelectChannel.setControlStep(SelectHostChannel
					.getInt("ControlStep"));
			cls_SelectChannel.setHostID(SelectHostChannel.getInt("HostID"));
			cls_SelectChannel.setRecommendID(SelectHostChannel
					.getInt("RecommendID"));
			cls_SelectChannel.setRecTime(SelectHostChannel.getInt("RecTime"));
			cls_SelectChannel.setPTZType(SelectHostChannel.getInt("PTZType"));
			cls_SelectChannel.SetStreamNo(SelectHostChannel.getInt("StreamNo"));

			m_textVideoDeviceName.setText("添加成功");

			break;
		case siDownload: // 图像参数设置
//			m_cls_VideoViewController.Registe(this);
			break;
		default:

			break;
		}
	}

	// 重载按键操作
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (m_cls_VideoViewController.HasPlayed()) {
				if (m_cls_VideoViewController.HasPlayed())
					m_cls_VideoViewController.StopVideo();
				else
					m_cls_VideoViewController.Logoff();
				finish();
				return true;
			} else {
				m_cls_VideoViewController.Logoff();
			}

		}

		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {

		super.onConfigurationChanged(newConfig);
		SuitScreen();
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		return new AlertDialog.Builder(this).setMessage("ddddd").create();

	}

	private boolean Orientation() {
		int iDirection = 1; // 1 --放大 ； 0 --缩小
		int iWidth = m_iAfterX - m_iPreX;
		int iHeight = m_iAfterY - m_iPreY;
		if (iWidth < 0) {
			iDirection = 0;
			// iWidth *= -1;
		}
		// if(iHeight < 0)
		// {
		// iHeight *= -1;
		// }
		int iNewCenterX = (int) iWidth / 2 + m_iPreX;
		int iNewCenterY = (int) iHeight / 2 + m_iPreY;

		m_cls_VideoViewController.Device3DCtrl(iNewCenterX, iNewCenterY,
				m_iFullVideoWidth, m_iFullVideoHeight, iDirection);

		m_iPreX = -1;
		m_iPreY = -1;
		m_iAfterX = -1;
		m_iAfterY = -1;
		return false;
	}

	// 子类化一个Handler

	public class MsgHandler extends Handler {
		int icount = 0;

		public MsgHandler(Looper looper) {

			super(looper);

		}

		@Override
		public void handleMessage(Message msg) {
			int iswitch = msg.arg1;
			// int _iLParam = msg.arg2;
			//
			// m_textVideoDeviceName.setText("_iWparam=" + iswitch
			// +",icount="+icount++);
			// m_ViewProgress.setVisibility(View.INVISIBLE);
			// return;
			switch (iswitch) {
			case -3:
				m_textVideoDeviceName
						.setText(getString(R.string.Notes_Logon_Timeout));
				m_ViewProgress.setVisibility(View.INVISIBLE);
				break;
			case -2:
				m_textVideoDeviceName
						.setText(getString(R.string.Notes_Rev_Failed));
				m_ViewProgress.setVisibility(View.INVISIBLE);
				break;
			case -1:
				m_textVideoDeviceName
						.setText(getString(R.string.Notes_Logon_Failed));
				m_ViewProgress.setVisibility(View.INVISIBLE);
				break;
			case 0: // 登录成功开始接受视频
				m_textVideoDeviceName
						.setText(getString(R.string.Notes_Logon_Success));
				m_ViewProgress.setVisibility(View.INVISIBLE);
				// ==========================>点击开始播放动作
				if (cls_SelectHost == null || cls_SelectChannel == null) { // 请先添加设备信息！
					Toast.makeText(Show_Video_View.this,
							R.string.Noate_Add_Device, Toast.LENGTH_SHORT)
							.show();
					return;
				}
				if (!m_cls_VideoViewController.HasRev()) { // 请登录设备
					Toast.makeText(Show_Video_View.this,
							R.string.Noate_Logon_Device, Toast.LENGTH_SHORT)
							.show();
					return;
				}
				m_cls_VideoViewController.PlaySelctedChannel();
				break;
			case 1: // 开始播放视频
				m_ViewProgress.setVisibility(View.INVISIBLE);
				String strDeviceName = m_cls_VideoViewController
						.GetSelectHostName();
				String strChannelName = m_cls_VideoViewController
						.GetSelectChannelName();
				if (!strDeviceName.equalsIgnoreCase("")) {
					if (!strChannelName.equalsIgnoreCase(""))
						m_textVideoDeviceName.setText(strDeviceName + " - "
								+ strChannelName);
					else
						m_textVideoDeviceName.setText(strDeviceName);
				}
				break;
			case 2: // 接收显示GPS信息
				m_ViewProgress.setVisibility(View.INVISIBLE);
				String strGPSInfo = m_cls_VideoViewController.getStrGPSInfo();
				m_textVideoDeviceName.setText(strGPSInfo);

				break;

			default:
			}
		}

	}
}
